package de.tto.dating.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.servlet.ServletContext;

public class FileService {

	public synchronized String readFile(ServletContext context, String path)
			throws IOException {

		InputStream is = context.getResourceAsStream(path);
		if (is == null) {
			return null;
		}

		InputStreamReader isr = new InputStreamReader(is);
		Reader reader = new BufferedReader(isr);

		try {
			StringBuilder sb = new StringBuilder();
			char[] buf = new char[1024];
			int charsRead;
			while ((charsRead = reader.read(buf)) != -1) {
				sb.append(buf, 0, charsRead);
			}
			return sb.toString();
		} finally {
			reader.close();
		}
	}
}
